package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.entity.SysBookType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysBookTypeMapper;
import com.ruoyi.system.service.ISysBookTypeService;

/**
 * 图书类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-14
 */
@Service
public class SysBookTypeServiceImpl implements ISysBookTypeService 
{
    @Autowired
    private SysBookTypeMapper sysBookTypeMapper;

    /**
     * 查询图书类型
     * 
     * @param typeId 图书类型主键
     * @return 图书类型
     */
    @Override
    public SysBookType selectSysBookTypeByTypeId(Long typeId)
    {
        return sysBookTypeMapper.selectSysBookTypeByTypeId(typeId);
    }

    /**
     * 查询图书类型列表
     * 
     * @param sysBookType 图书类型
     * @return 图书类型
     */
    @Override
    public List<SysBookType> selectSysBookTypeList(SysBookType sysBookType)
    {
        return sysBookTypeMapper.selectSysBookTypeList(sysBookType);
    }

    /**
     * 新增图书类型
     * 
     * @param sysBookType 图书类型
     * @return 结果
     */
    @Override
    public int insertSysBookType(SysBookType sysBookType)
    {
        SysBookType info = sysBookTypeMapper.selectSysBookTypeByTypeId(sysBookType.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
        {
            throw new ServiceException("部门停用，不允许新增");
        }
        sysBookType.setAncestors(info.getAncestors() + "," + sysBookType.getParentId());
        sysBookType.setCreateTime(DateUtils.getNowDate());
        return sysBookTypeMapper.insertSysBookType(sysBookType);
    }

    /**
     * 修改图书类型
     * 
     * @param sysBookType 图书类型
     * @return 结果
     */
    @Override
    public int updateSysBookType(SysBookType sysBookType)
    {
        sysBookType.setUpdateTime(DateUtils.getNowDate());
        return sysBookTypeMapper.updateSysBookType(sysBookType);
    }

    /**
     * 批量删除图书类型
     * 
     * @param typeIds 需要删除的图书类型主键
     * @return 结果
     */
    @Override
    public int deleteSysBookTypeByTypeIds(Long[] typeIds)
    {
        return sysBookTypeMapper.deleteSysBookTypeByTypeIds(typeIds);
    }

    /**
     * 删除图书类型信息
     * 
     * @param typeId 图书类型主键
     * @return 结果
     */
    @Override
    public int deleteSysBookTypeByTypeId(Long typeId)
    {
        return sysBookTypeMapper.deleteSysBookTypeByTypeId(typeId);
    }

    @Override
    public List<SysBookType> buildsysBookTypeTree(List<SysBookType> sysBookTypes) {
        List<SysBookType> returnList = new ArrayList<SysBookType>();
        List<Long> tempList = new ArrayList<Long>();
        for (SysBookType bookType : sysBookTypes)
        {
            tempList.add(bookType.getTypeId());
        }
        for (Iterator<SysBookType> iterator = sysBookTypes.iterator(); iterator.hasNext();)
        {
            SysBookType bookType = (SysBookType) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(bookType.getParentId()))
            {
                recursionFn(sysBookTypes, bookType);
                returnList.add(bookType);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = sysBookTypes;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysBookType> list, SysBookType t)
    {
        // 得到子节点列表
        List<SysBookType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysBookType tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    @Override
    public List<TreeSelect> buildsysBookTypeTreeSelect(List<SysBookType> sysBookTypes) {
        List<SysBookType> bookTypeTrees = buildsysBookTypeTree(sysBookTypes);
        return bookTypeTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public String checkbookTypeNameUnique(SysBookType sysBookType) {
        Long deptId = StringUtils.isNull(sysBookType.getTypeId()) ? -1L : sysBookType.getTypeId();
        SysBookType info = sysBookTypeMapper.checkbookTypeNameUnique(sysBookType.getTypeName(), sysBookType.getParentId());
        if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != deptId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 得到子节点列表
     */
    private List<SysBookType> getChildList(List<SysBookType> list, SysBookType t)
    {
        List<SysBookType> tlist = new ArrayList<SysBookType>();
        Iterator<SysBookType> it = list.iterator();
        while (it.hasNext())
        {
            SysBookType n = (SysBookType) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getTypeId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysBookType> list, SysBookType t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
